package com.ithemia.servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

public class DemoServlet extends HttpServlet {
    private ServletConfig config;
    private String age;

    /*
    初始化方法: 获取初始参数信息
     */
    @Override
    public void init(ServletConfig config) throws ServletException {
        //通过初始化参数配置中的 <param-name>  获取 <param-value>的值
        age = config.getInitParameter("age");
        String name = config.getInitParameter("name");
        System.out.println(age + "--" + name);

        this.config = config;
    }

    //自调用方法
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("servlet.....");

        ServletContext context = getServletContext();
        //有虚拟路径,获取配置的虚拟路径  没有则表示根目录
        String contextPath = context.getContextPath();

        System.out.println(contextPath);

        //文件上传可指定路径创建
        //指定服务器部署工程下的目录或文件
        String realPath = context.getRealPath("/pic");

        File file = new File(realPath);
        if (!file.exists()){
            file.mkdirs();
        }


    }
}
